1 module hip.gui.linear_layout;
2 public import hip.gui.widget;
3 
4 
5 class LinearLayout : Widget
6 {
7     enum Direction
8     {
9         horizontal,
10         vertical
11     }
12 
13     protected Direction dir;
14     protected int spacing = 0;
15     protected int padding = 0;
16 
17     public void setDirection(Direction d)
18     {
19         dir = d;
20         updateLayout();
21     }
22     void setSpacing(int spacing)
23     {
24         this.spacing = spacing;
25         updateLayout();
26     }
27     void setPadding(int padding)
28     {
29         this.padding = padding;
30         updateLayout();
31     }
32 
33     alias addChild = Widget.addChild;
34     override void addChild(Widget w)
35     {
36         super.addChild(w);
37         updateLayout();
38     }
39 
40 
41     void updateLayout()
42     {
43         int x, y;
44         foreach(ch; children)
45         {
46             import hip.api;
47             if(!ch.visible) continue;
48             ch.setPosition(x,y);
49             if(dir == Direction.horizontal)
50                 x+= spacing + ch.width;
51             else 
52                 y+= spacing + ch.height;
53         }
54     }
55     override void preRender()
56     {
57         super.preRender();
58         updateLayout();
59     }
60 }